import {
  FieldMetadataType,
  RelationOnDeleteAction,
  RelationType,
} from 'twenty-shared/types';

import { type FlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/types/flat-field-metadata.type';
import { type AllStandardObjectFieldName } from 'src/engine/workspace-manager/twenty-standard-application/types/all-standard-object-field-name.type';
import { createStandardFieldFlatMetadata } from 'src/engine/workspace-manager/twenty-standard-application/utils/create-standard-field-flat-metadata.util';
import { createStandardRelationFieldFlatMetadata } from 'src/engine/workspace-manager/twenty-standard-application/utils/create-standard-relation-field-flat-metadata.util';
import { type StandardFieldMetadataIdByObjectAndFieldName } from 'src/engine/workspace-manager/twenty-standard-application/utils/get-standard-field-metadata-id-by-object-and-field-name.util';

export const buildCalendarChannelStandardFlatFieldMetadatas = ({
  createdAt,
  workspaceId,
  standardFieldMetadataIdByObjectAndFieldName,
}: {
  createdAt: Date;
  workspaceId: string;
  standardFieldMetadataIdByObjectAndFieldName: StandardFieldMetadataIdByObjectAndFieldName;
}): Record<
  AllStandardObjectFieldName<'calendarChannel'>,
  FlatFieldMetadata
> => ({
  id: createStandardFieldFlatMetadata({
    objectName: 'calendarChannel',
    workspaceId,
    options: {
      fieldName: 'id',
      type: FieldMetadataType.UUID,
      label: 'Id',
      description: 'Id',
      icon: 'Icon123',
      isSystem: true,
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'uuid',
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  createdAt: createStandardFieldFlatMetadata({
    objectName: 'calendarChannel',
    workspaceId,
    options: {
      fieldName: 'createdAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Creation date',
      description: 'Creation date',
      icon: 'IconCalendar',
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'now',
      settings: { displayFormat: 'RELATIVE' },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  updatedAt: createStandardFieldFlatMetadata({
    objectName: 'calendarChannel',
    workspaceId,
    options: {
      fieldName: 'updatedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Last update',
      description: 'Last time the record was changed',
      icon: 'IconCalendarClock',
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'now',
      settings: { displayFormat: 'RELATIVE' },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  deletedAt: createStandardFieldFlatMetadata({
    objectName: 'calendarChannel',
    workspaceId,
    options: {
      fieldName: 'deletedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Deleted at',
      description: 'Date when the record was deleted',
      icon: 'IconCalendarMinus',
      isNullable: true,
      isUIReadOnly: true,
      settings: { displayFormat: 'RELATIVE' },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  handle: createStandardFieldFlatMetadata({
    objectName: 'calendarChannel',
    workspaceId,
    options: {
      fieldName: 'handle',
      type: FieldMetadataType.TEXT,
      label: 'Handle',
      description: 'Handle',
      icon: 'IconAt',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  visibility: createStandardFieldFlatMetadata({
    objectName: 'calendarChannel',
    workspaceId,
    options: {
      fieldName: 'visibility',
      type: FieldMetadataType.SELECT,
      label: 'Visibility',
      description: 'Visibility',
      icon: 'IconEyeglass',
      isNullable: false,
      defaultValue: "'SHARE_EVERYTHING'",
      options: [
        { value: 'METADATA', label: 'Metadata', position: 0, color: 'green' },
        {
          value: 'SHARE_EVERYTHING',
          label: 'Share Everything',
          position: 1,
          color: 'orange',
        },
      ],
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  isContactAutoCreationEnabled: createStandardFieldFlatMetadata({
    objectName: 'calendarChannel',
    workspaceId,
    options: {
      fieldName: 'isContactAutoCreationEnabled',
      type: FieldMetadataType.BOOLEAN,
      label: 'Is Contact Auto Creation Enabled',
      description: 'Is Contact Auto Creation Enabled',
      icon: 'IconUserCircle',
      isNullable: false,
      defaultValue: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  contactAutoCreationPolicy: createStandardFieldFlatMetadata({
    objectName: 'calendarChannel',
    workspaceId,
    options: {
      fieldName: 'contactAutoCreationPolicy',
      type: FieldMetadataType.SELECT,
      label: 'Contact auto creation policy',
      description:
        'Automatically create records for people you participated with in an event.',
      icon: 'IconUserCircle',
      isNullable: false,
      defaultValue: "'AS_PARTICIPANT_AND_ORGANIZER'",
      options: [
        {
          value: 'AS_PARTICIPANT_AND_ORGANIZER',
          label: 'As Participant and Organizer',
          color: 'green',
          position: 0,
        },
        {
          value: 'AS_PARTICIPANT',
          label: 'As Participant',
          color: 'orange',
          position: 1,
        },
        {
          value: 'AS_ORGANIZER',
          label: 'As Organizer',
          color: 'blue',
          position: 2,
        },
        { value: 'NONE', label: 'None', color: 'red', position: 3 },
      ],
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  isSyncEnabled: createStandardFieldFlatMetadata({
    objectName: 'calendarChannel',
    workspaceId,
    options: {
      fieldName: 'isSyncEnabled',
      type: FieldMetadataType.BOOLEAN,
      label: 'Is Sync Enabled',
      description: 'Is Sync Enabled',
      icon: 'IconRefresh',
      isNullable: false,
      defaultValue: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  syncCursor: createStandardFieldFlatMetadata({
    objectName: 'calendarChannel',
    workspaceId,
    options: {
      fieldName: 'syncCursor',
      type: FieldMetadataType.TEXT,
      label: 'Sync Cursor',
      description:
        'Sync Cursor. Used for syncing events from the calendar provider',
      icon: 'IconReload',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  syncStatus: createStandardFieldFlatMetadata({
    objectName: 'calendarChannel',
    workspaceId,
    options: {
      fieldName: 'syncStatus',
      type: FieldMetadataType.SELECT,
      label: 'Sync status',
      description: 'Sync status',
      icon: 'IconStatusChange',
      isNullable: true,
      options: [
        { value: 'ONGOING', label: 'Ongoing', position: 1, color: 'yellow' },
        {
          value: 'NOT_SYNCED',
          label: 'Not Synced',
          position: 2,
          color: 'blue',
        },
        { value: 'ACTIVE', label: 'Active', position: 3, color: 'green' },
        {
          value: 'FAILED_INSUFFICIENT_PERMISSIONS',
          label: 'Failed Insufficient Permissions',
          position: 4,
          color: 'red',
        },
        {
          value: 'FAILED_UNKNOWN',
          label: 'Failed Unknown',
          position: 5,
          color: 'red',
        },
      ],
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  syncStage: createStandardFieldFlatMetadata({
    objectName: 'calendarChannel',
    workspaceId,
    options: {
      fieldName: 'syncStage',
      type: FieldMetadataType.SELECT,
      label: 'Sync stage',
      description: 'Sync stage',
      icon: 'IconStatusChange',
      isNullable: false,
      defaultValue: "'PENDING_CONFIGURATION'",
      options: [
        {
          value: 'CALENDAR_EVENT_LIST_FETCH_PENDING',
          label: 'Calendar event list fetch pending',
          position: 0,
          color: 'blue',
        },
        {
          value: 'CALENDAR_EVENT_LIST_FETCH_SCHEDULED',
          label: 'Calendar event list fetch scheduled',
          position: 1,
          color: 'green',
        },
        {
          value: 'CALENDAR_EVENT_LIST_FETCH_ONGOING',
          label: 'Calendar event list fetch ongoing',
          position: 2,
          color: 'orange',
        },
        {
          value: 'CALENDAR_EVENTS_IMPORT_PENDING',
          label: 'Calendar events import pending',
          position: 3,
          color: 'blue',
        },
        {
          value: 'CALENDAR_EVENTS_IMPORT_SCHEDULED',
          label: 'Calendar events import scheduled',
          position: 4,
          color: 'green',
        },
        {
          value: 'CALENDAR_EVENTS_IMPORT_ONGOING',
          label: 'Calendar events import ongoing',
          position: 5,
          color: 'orange',
        },
        { value: 'FAILED', label: 'Failed', position: 6, color: 'red' },
        {
          value: 'PENDING_CONFIGURATION',
          label: 'Pending configuration',
          position: 9,
          color: 'gray',
        },
      ],
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  syncStageStartedAt: createStandardFieldFlatMetadata({
    objectName: 'calendarChannel',
    workspaceId,
    options: {
      fieldName: 'syncStageStartedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Sync stage started at',
      description: 'Sync stage started at',
      icon: 'IconHistory',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  syncedAt: createStandardFieldFlatMetadata({
    objectName: 'calendarChannel',
    workspaceId,
    options: {
      fieldName: 'syncedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Last sync date',
      description: 'Last sync date',
      icon: 'IconHistory',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  throttleFailureCount: createStandardFieldFlatMetadata({
    objectName: 'calendarChannel',
    workspaceId,
    options: {
      fieldName: 'throttleFailureCount',
      type: FieldMetadataType.NUMBER,
      label: 'Throttle Failure Count',
      description: 'Throttle Failure Count',
      icon: 'IconX',
      isNullable: false,
      defaultValue: 0,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  connectedAccount: createStandardRelationFieldFlatMetadata({
    objectName: 'calendarChannel',
    workspaceId,
    options: {
      fieldName: 'connectedAccount',
      label: 'Connected Account',
      description: 'Connected Account',
      icon: 'IconUserCircle',
      isNullable: false,
      createdAt,
      targetObjectName: 'connectedAccount',
      targetFieldName: 'calendarChannels',
      settings: {
        relationType: RelationType.MANY_TO_ONE,
        onDelete: RelationOnDeleteAction.CASCADE,
        joinColumnName: 'connectedAccountId',
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  calendarChannelEventAssociations: createStandardRelationFieldFlatMetadata({
    objectName: 'calendarChannel',
    workspaceId,
    options: {
      fieldName: 'calendarChannelEventAssociations',
      label: 'Calendar Channel Event Associations',
      description: 'Calendar Channel Event Associations',
      icon: 'IconCalendar',
      isNullable: false,
      createdAt,
      targetObjectName: 'calendarChannelEventAssociation',
      targetFieldName: 'calendarChannel',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
});
